package com.burgstaller.aGTD;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import android.content.AbstractTableMerger;
import android.content.ContentValues;
import android.content.Context;
import android.content.DatabaseContentProvider;
import android.content.Resources;
import android.content.SyncAdapter;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.burgstaller.aGTD.sync.AgtdSyncAdapter;
import com.burgstaller.provider.AGtd;
import com.google.wireless.gdata.data.StringUtils;

/**
 * this class serves the content for the context table
 * @author rainer
 */
public class AGtdContentProvider extends DatabaseContentProvider {
	/** the log context tag for log output strings. */
	public static final String TAG = AGtd.LOG_TAG;
	private static final String CURSOR_LOG = "AGTDCUR";


	private static UriMatcher s_urlMatcher;

	private static HashMap<String, String> s_contextListProjectionMap;

	private static HashMap<String, String> s_taskProjectionMap;

	private static HashMap<String, String> s_projectListProjectionMap;

	/** the uri modes for the uri parser */
	private static final int CONTEXTS = 0;
	private static final int CONTEXT_ID = 1;
	private static final int TASKS = 2;
	private static final int TASK_ID = 3;
	private static final int TASKS_FOR_CONTEXT = 4;
	private static final int PROJECTS = 5;
	private static final int PROJECT_ID = 6;
	private static final int TASKS_FOR_PROJECT = 7;
	private static final int PROJECT_TREE = 8;

	// GENERIC TABLE/DB INFO
	private static final String DATABASE_NAME = "agtd.db";
	private static final int DATABASE_VERSION = 1;
	private static final String CONTEXT_TABLE_NAME = "contexts";
	private static final String TASK_TABLE_NAME = "tasks";
	private static final String PROJECTS_TABLE_NAME = "projects";


	private AgtdSyncAdapter m_syncAdapter;
	
	public AGtdContentProvider() {
		super(DATABASE_NAME, DATABASE_VERSION);
	}

	private static UriMatcher getParser() {
		if (s_urlMatcher == null) {
			s_urlMatcher = new UriMatcher(UriMatcher.NO_MATCH);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "contexts", CONTEXTS);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "contexts/#", CONTEXT_ID);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "tasks", TASKS);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "tasks/#", TASK_ID);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "projects", PROJECTS);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "projects/#", PROJECT_ID);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "projectTree/#", PROJECT_TREE);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "tasksForContext/#", 
					TASKS_FOR_CONTEXT);
			s_urlMatcher.addURI(AGtd.AUTHORITY, "tasksForProject/#", 
					TASKS_FOR_PROJECT);
		}
		return s_urlMatcher;
	}

	private static HashMap<String, String> getContextProjectionMap() {
		if (s_contextListProjectionMap == null) {
			s_contextListProjectionMap = new HashMap<String, String>();
			s_contextListProjectionMap.put(AGtd.Contexts._ID, "_id");
			s_contextListProjectionMap.put(AGtd.Contexts.TITLE, "title");
			s_contextListProjectionMap.put(AGtd.Contexts.NOTES, "notes");
			s_contextListProjectionMap.put(AGtd.Contexts.CREATED_DATE,
					"created");
			s_contextListProjectionMap.put(AGtd.Contexts.MODIFIED_DATE,
					"modified");
			s_contextListProjectionMap.put(AGtd.Contexts.SYNC_DATE, 
					"last_synced");
			s_contextListProjectionMap.put(AGtd.Contexts.SYNC_STATUS, 
					AGtd.Contexts.SYNC_STATUS);
			s_contextListProjectionMap.put(AGtd.Contexts.EXTERNAL_ID, 
					"external_id");
		}
		return s_contextListProjectionMap;
	}

	private static HashMap<String, String> getProjectProjectionMap() {
		if (s_projectListProjectionMap == null) {
			s_projectListProjectionMap = new HashMap<String, String>();
			s_projectListProjectionMap.put(AGtd.Projects._ID, "_id");
			s_projectListProjectionMap.put(AGtd.Projects.TITLE, "title");
			s_projectListProjectionMap.put(AGtd.Projects.NOTES, "notes");
			s_projectListProjectionMap.put(AGtd.Projects.CREATED_DATE,
					"created");
			s_projectListProjectionMap.put(AGtd.Projects.MODIFIED_DATE,
					"modified");
			s_projectListProjectionMap.put(AGtd.Projects.COMPLETED, 
					"completed");
			s_projectListProjectionMap.put(AGtd.Projects.SYNC_DATE, 
				"last_synced");
			s_projectListProjectionMap.put(AGtd.Projects.SYNC_STATUS, 
				"sync_status");
			s_projectListProjectionMap.put(AGtd.Projects.EXTERNAL_ID, 
				"external_id");
			s_projectListProjectionMap.put(AGtd.Projects.FK_PARENTPROJ_ID, 
					"parentproj_id");
		}
		return s_projectListProjectionMap;
	}
	private static HashMap<String, String> getTaskProjectionMap() {
		if (s_taskProjectionMap == null) {
			s_taskProjectionMap = new HashMap<String, String>();
			s_taskProjectionMap.put(AGtd.Tasks._ID, "_id");
			s_taskProjectionMap.put(AGtd.Tasks.TITLE, "title");
			s_taskProjectionMap.put(AGtd.Tasks.NOTES, "notes");
			s_taskProjectionMap.put(AGtd.Tasks.START_DATE, "start");
			s_taskProjectionMap.put(AGtd.Tasks.DUE_DATE, "due");
			s_taskProjectionMap.put(AGtd.Tasks.FK_CONTEXT_ID, "context_id");
			s_taskProjectionMap.put(AGtd.Tasks.FK_PROJECT_ID, "project_id");
			s_taskProjectionMap.put(AGtd.Tasks.CREATED_DATE,
					"created");
			s_taskProjectionMap.put(AGtd.Tasks.MODIFIED_DATE,
					"modified");
			s_taskProjectionMap.put(AGtd.Tasks.COMPLETED, "completed");
			s_taskProjectionMap.put(AGtd.Tasks.SYNC_DATE, 
				"last_synced");
			s_taskProjectionMap.put(AGtd.Tasks.SYNC_STATUS, 
				"sync_status");
			s_taskProjectionMap.put(AGtd.Tasks.EXTERNAL_ID, 
				"external_id");
			s_taskProjectionMap.put(AGtd.Tasks.EXTRA_SYNC_INFO, "extra_sync");
			
		}
		return s_taskProjectionMap;
	}

	
	private static HashMap<Cursor, Throwable> s_cursorStack = new HashMap<Cursor, Throwable>();
	
	public static void pushCursor(Cursor c, String message) {
		Throwable t = new Throwable(message);
		Log.i(CURSOR_LOG, "cursor: push " + c, t);
		s_cursorStack.put(c, t);
	}
	public static void popCursor(Cursor c) {
		Throwable t = s_cursorStack.get(c);
		Log.i(CURSOR_LOG, "cursor: pop " + c, t);
		s_cursorStack.remove(c);
	}
	public static void debugCursorStack() {
		Log.d(AGtd.LOG_TAG, "Current Cursor Stack");
		for (Cursor c : s_cursorStack.keySet()) {
			Throwable t = s_cursorStack.get(c);
			Log.d(CURSOR_LOG, "  Cursor: " + c, t);
		}
	}
	
	@Override
	public int deleteInternal(Uri url, String where, String[] whereArgs) {
		int count;
		String segment = null;
		
		
		Log.d(AGtd.LOG_TAG, "Delete: " + url + ", where: " + where);
		for (String arg: whereArgs) {
			Log.d(AGtd.LOG_TAG, "    arg: " + arg);
		}
		switch (getParser().match(url)) {
		case CONTEXTS:
			count = getDatabase().delete(CONTEXT_TABLE_NAME, where, whereArgs);
			break;

		case CONTEXT_ID:
			segment = url.getLastPathSegment();
			count = getDatabase().delete(CONTEXT_TABLE_NAME,
					"_id="	+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		case PROJECTS:
		case PROJECT_TREE:
			count = getDatabase().delete(PROJECTS_TABLE_NAME, where, whereArgs);
			break;

		case PROJECT_ID:
			segment = url.getLastPathSegment();
			count = getDatabase().delete(PROJECTS_TABLE_NAME,
					"_id="	+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		case TASKS_FOR_PROJECT:
		case TASKS_FOR_CONTEXT:
		case TASKS:
			count = getDatabase().delete(TASK_TABLE_NAME, where, whereArgs);
			break;
			
		case TASK_ID:
			segment = url.getLastPathSegment();
			count = getDatabase().delete(TASK_TABLE_NAME, AGtd.Tasks._ID + "=" + segment
					+ (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""),
					whereArgs);
			break;
			
			
		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}

		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		Log.i(AGtd.LOG_TAG, "getType: " + uri);
		
		switch (getParser().match(uri)) {
		case CONTEXTS:
			return "vnd.android.cursor.dir/vnd.burgstaller.context";

		case CONTEXT_ID:
			return "vnd.android.cursor.item/vnd.burgstaller.context";

		case PROJECT_TREE:
		case PROJECTS:
			return "vnd.android.cursor.dir/vnd.burgstaller.project";

		case PROJECT_ID:
			return "vnd.android.cursor.item/vnd.burgstaller.project";

		case TASKS:
		case TASKS_FOR_CONTEXT:
		case TASKS_FOR_PROJECT:
			Log.i(AGtd.LOG_TAG, " returned type: " +
					"vnd.android.cursor.dir/vnd.burgstaller.task");
			return "vnd.android.cursor.dir/vnd.burgstaller.task";

		case TASK_ID:
			return "vnd.android.cursor.item/vnd.burgstaller.task";
			
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}

	@Override
	public Uri insertInternal(Uri url, ContentValues initialValues) {
		long rowID;
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}
		
		int dataMode = getParser().match(url);
		Uri uriOfNewElement = null;
		

		// verify that we have the proper data mode
		switch (dataMode) {
		case TASKS_FOR_CONTEXT:
		case TASKS_FOR_PROJECT:
		case TASKS:
			uriOfNewElement = AGtd.Tasks.CONTENT_URI;
			if (!values.containsKey(AGtd.Tasks.FK_CONTEXT_ID)) {
				values.put(AGtd.Tasks.FK_CONTEXT_ID, AGtd.Contexts.INBOX_RECORD_ID);
			}
			break;
		case CONTEXTS:
			uriOfNewElement = AGtd.Contexts.CONTENT_URI;
			break;
		case PROJECT_TREE:
		case PROJECTS:
			uriOfNewElement = AGtd.Projects.CONTENT_URI;
			break;
			
		default:
			throw new IllegalArgumentException("Unknown URL " + url);			
		}
		Long now = Long.valueOf(System.currentTimeMillis());
		Resources r = Resources.getSystem();

		// Make sure that the fields are all set
		if (values.containsKey(AGtd.CommonColumns.CREATED_DATE) == false) {
			values.put(AGtd.Contexts.CREATED_DATE, now);
		}

		if (values.containsKey(AGtd.CommonColumns.MODIFIED_DATE) == false) {
			values.put(AGtd.Contexts.MODIFIED_DATE, now);
		}
		
		if (!values.containsKey(AGtd.CommonColumns.SYNC_STATUS)) {
			values.put(AGtd.CommonColumns.SYNC_STATUS, 
					AGtd.CommonColumns.SYNC_STATUS_NEW);
		}

//		if (values.containsKey(AGtd.Contexts.TITLE) == false) {
//			values.put(AGtd.Contexts.TITLE, r
//					.getString(android.R.string.untitled));
//		}

		if (values.containsKey(AGtd.CommonColumns.NOTES) == false) {
			values.put(AGtd.CommonColumns.NOTES, "");
		}

		String tableName = addTableSpecificDefaults(values, dataMode, url);
		
		Log.i(AGtd.LOG_TAG, "inserting into " + tableName);
		dumpValues(values);
		rowID = getDatabase().insert(tableName, tableName, values);
		if (rowID > 0) {
			Uri uri = Uri.withAppendedPath(uriOfNewElement, "" + rowID);
			getContext().getContentResolver().notifyChange(uri, null);
			return uri;
		}

		throw new SQLException("Failed to insert row into " + url);
	}

	private String addTableSpecificDefaults(ContentValues values, 
			int dataMode, Uri url) {
		switch (dataMode) {
		case TASKS:
			return TASK_TABLE_NAME;
		case CONTEXTS:
			return CONTEXT_TABLE_NAME;
		case PROJECT_TREE:
			// the last part of the url contains the parent project id
			if (!values.containsKey(AGtd.Projects.FK_PARENTPROJ_ID)) {
				values.put(AGtd.Projects.FK_PARENTPROJ_ID, 
						url.getLastPathSegment());
			}
			// fall through
		case PROJECTS:
			if (!values.containsKey(AGtd.Projects.FK_PARENTPROJ_ID)) {
				values.put(AGtd.Projects.FK_PARENTPROJ_ID, -1);
			}
			return PROJECTS_TABLE_NAME;
			
		case TASKS_FOR_PROJECT:
			if (!values.containsKey(AGtd.Tasks.FK_PROJECT_ID)) {
				values.put(AGtd.Tasks.FK_PROJECT_ID, url.getLastPathSegment());
			}
			return TASK_TABLE_NAME;
		case TASKS_FOR_CONTEXT:
			if (!values.containsKey(AGtd.Tasks.FK_CONTEXT_ID)) {
				values.put(AGtd.Tasks.FK_CONTEXT_ID, url.getLastPathSegment());
			}
			return TASK_TABLE_NAME;
		}
		return null;
	}

//	@Override
//	public boolean onCreate() {
//		DatabaseHelper helper = new DatabaseHelper();
//		m_DB = helper.openDatabase(getContext(), DATABASE_NAME, null,
//				DATABASE_VERSION);
//		if (m_DB == null) {
//			return false;
//		} else {
//			return true;
//		}
//	}

	@Override
	protected Cursor queryInternal(Uri url, String[] projection, String selection,
			String[] selectionArg, String sort) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		

		switch (getParser().match(url)) {
		case CONTEXTS:
			qb.setTables(CONTEXT_TABLE_NAME);
			qb.setProjectionMap(getContextProjectionMap());
			break;

		case CONTEXT_ID:
			qb.setTables(CONTEXT_TABLE_NAME);
			qb.appendWhere("_id=" + url.getLastPathSegment());
			break;

		case PROJECT_TREE:
			qb.appendWhere(AGtd.Projects.FK_PARENTPROJ_ID + "=" + 
					url.getLastPathSegment());
			// fall through
		case PROJECTS:
			qb.setTables(PROJECTS_TABLE_NAME);
			qb.setProjectionMap(getContextProjectionMap());
			break;

			
		case PROJECT_ID:
			qb.setTables(PROJECTS_TABLE_NAME);
			qb.appendWhere("_id=" + url.getLastPathSegment());
			break;

		case TASKS:
			qb.setTables(TASK_TABLE_NAME);
			qb.setProjectionMap(getTaskProjectionMap());
			break;
			
		case TASK_ID:
			qb.setTables(TASK_TABLE_NAME);
			qb.appendWhere(AGtd.CommonColumns._ID + "=" 
					+ url.getLastPathSegment());
			break;
			
		case TASKS_FOR_CONTEXT:
			qb.setTables(TASK_TABLE_NAME);
			qb.appendWhere(AGtd.Tasks.FK_CONTEXT_ID + "=" 
					+ url.getLastPathSegment());
			qb.appendWhere(" AND ");
			qb.appendWhere(AGtd.Tasks.SYNC_STATUS + "!=" 
					+ AGtd.Tasks.SYNC_STATUS_DELETED);
			break;

		case TASKS_FOR_PROJECT:
			qb.setTables(TASK_TABLE_NAME);
			qb.appendWhere(AGtd.Tasks.FK_PROJECT_ID + "=" 
					+ url.getLastPathSegment());
			qb.appendWhere(" AND ");
			qb.appendWhere(AGtd.Tasks.SYNC_STATUS + "!=" 
					+ AGtd.Tasks.SYNC_STATUS_DELETED);
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}

		// If no sort order is specified use the default
		String orderBy;
		if (TextUtils.isEmpty(sort)) {
			orderBy = AGtd.CommonColumns.DEFAULT_SORT_ORDER;
		} else {
			orderBy = sort;
		}

		Log.d(AGtd.LOG_TAG, "query(" + url + ", selection:" + selection); 
		Cursor c = qb.query(getDatabase(), projection, selection, selectionArg,
				null, null, orderBy);
		c.setNotificationUri(getContext().getContentResolver(), url);
		pushCursor(c, "query(" + url + ", selection:" + selection
				+ ", count: " + c.count() + ": " + c); 
		return c;
	}

	@Override
	public int updateInternal(Uri url, ContentValues values, String where,
			String[] whereArgs) {
		int count;
		String segment = null;
		int dataMode = getParser().match(url);
		Log.i(AGtd.LOG_TAG, "calling update, url: " + url + ", datamode: " 
				+ dataMode + ", values:"
				+ values.toString());
		
		if (!values.containsKey(AGtd.CommonColumns.MODIFIED_DATE)) {
			values.put(AGtd.CommonColumns.MODIFIED_DATE, 
					System.currentTimeMillis());
		}
		
		switch (dataMode) {
		case CONTEXTS:
			Log.i(AGtd.LOG_TAG, "updating table contexts: " + where);
			count = getDatabase().update(CONTEXT_TABLE_NAME, values, where, whereArgs);
			break;

		case CONTEXT_ID:
			segment = url.getLastPathSegment();
			Log.i(AGtd.LOG_TAG, "updating table contexts: " + where + " , segment : " + segment);
			count = getDatabase().update(CONTEXT_TABLE_NAME, values,
					"_id=" 	+ segment + (!TextUtils.isEmpty(where) ? 
							" AND (" + where + ')' : ""), whereArgs);
			break;
		case PROJECTS:
			Log.i(AGtd.LOG_TAG, "updating table contexts: " + where);
			count = getDatabase().update(PROJECTS_TABLE_NAME, values, where, whereArgs);
			break;

		case PROJECT_ID:
			segment = url.getLastPathSegment();
			Log.i(AGtd.LOG_TAG, "updating table contexts: " + where + " , segment : " + segment);
			count = getDatabase().update(PROJECTS_TABLE_NAME, values,
					"_id=" 	+ segment + (!TextUtils.isEmpty(where) ? 
							" AND (" + where + ')' : ""), whereArgs);
			break;			
		case TASKS_FOR_PROJECT:
		case TASKS_FOR_CONTEXT:
			Log.i(AGtd.LOG_TAG, "updating table tasks for context: " + where);
		case TASKS:
			Log.i(AGtd.LOG_TAG, "updating table tasks: " + where);			
			count = getDatabase().update(TASK_TABLE_NAME, values, where, whereArgs);
			break;
			
		case TASK_ID:
			segment = url.getLastPathSegment();
			count = getDatabase().update(TASK_TABLE_NAME, values,
					"_id=" 	+ segment + (!TextUtils.isEmpty(where) ? 
							" AND (" + where + ')' : ""), whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}

		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}
	
	@Override
	protected void bootstrapDatabase() {
		super.bootstrapDatabase();
		Log.w(TAG, "Bootstrapping DB");
		
		SQLiteDatabase db = getDatabase();
		db.execSQL("CREATE TABLE " + CONTEXT_TABLE_NAME + " (" 
				+ AGtd.Contexts._ID	+ " INTEGER PRIMARY KEY," 
				+ AGtd.Contexts.TITLE + " TEXT,"
				+ AGtd.Contexts.NOTES + " TEXT NULL,"
				+ AGtd.Contexts.SYNC_DATE + " INTEGER NULL,"
				+ AGtd.Contexts.SYNC_STATUS + " INTEGER,"
				+ AGtd.Contexts.CREATED_DATE + " INTEGER,"
				+ AGtd.Contexts.MODIFIED_DATE + " INTEGER" + ");");
		db.execSQL("CREATE TABLE " + PROJECTS_TABLE_NAME + " (" 
				+ AGtd.Projects._ID	+ " INTEGER PRIMARY KEY," 
				+ AGtd.Projects.TITLE + " TEXT,"
				+ AGtd.Projects.NOTES + " TEXT NULL,"
				+ AGtd.Projects.FK_PARENTPROJ_ID + " INTEGER,"
				+ AGtd.Projects.PATH_NAME + " TEXT NULL,"
				+ AGtd.Projects.COMPLETED + " INTEGER,"
				+ AGtd.Projects.SYNC_DATE + " INTEGER NULL,"
				+ AGtd.Projects.SYNC_STATUS + " INTEGER,"
				+ AGtd.Projects.CREATED_DATE + " INTEGER,"
				+ AGtd.Projects.MODIFIED_DATE + " INTEGER" + ");");
		db.execSQL("CREATE TABLE " + TASK_TABLE_NAME + " (" 
				+ AGtd.Tasks._ID	+ " INTEGER PRIMARY KEY," 
				+ AGtd.Tasks.TITLE + " TEXT,"
				+ AGtd.Tasks.NOTES + " TEXT,"
				+ AGtd.Tasks.FK_CONTEXT_ID + " INTEGER,"
				+ AGtd.Tasks.FK_PROJECT_ID + " INTEGER,"
				+ AGtd.Tasks.START_DATE + " INTEGER NULL,"
				+ AGtd.Tasks.DUE_DATE + " INTEGER NULL,"
				+ AGtd.Tasks.COMPLETED + " INTEGER,"
				+ AGtd.Tasks.SYNC_DATE + " INTEGER NULL,"
				+ AGtd.Tasks.SYNC_STATUS + " INTEGER,"
				+ AGtd.Tasks.EXTERNAL_ID + " INTEGER,"
				+ AGtd.Tasks.CREATED_DATE + " INTEGER,"
				+ AGtd.Tasks.MODIFIED_DATE + " INTEGER,"
				+ AGtd.Tasks.EXTRA_SYNC_INFO + " TEXT"
				+ ");");
		
		ContentValues inboxContext = new ContentValues();
		inboxContext.put(AGtd.Contexts.TITLE, "INBOX");
		inboxContext.put(AGtd.Contexts._ID, AGtd.Contexts.INBOX_RECORD_ID);
		db.insert(CONTEXT_TABLE_NAME, CONTEXT_TABLE_NAME, inboxContext);
//		insert(AGtd.Contexts.CONTENT_URI, inboxContext);
	}

	@Override
	protected void upgradeDatabase(int oldVersion, int newVersion) {
		Log.w(AGtd.LOG_TAG, "Upgrading database from version " + oldVersion 
				+ " to " + newVersion 
				+ ", which will destroy all old data");
		SQLiteDatabase db = getDatabase();
		db.execSQL("DROP TABLE IF EXISTS contexts");
		db.execSQL("DROP TABLE IF EXISTS tasks");
		db.execSQL("DROP TABLE IF EXISTS projects");
		bootstrapDatabase();
		// for now this is the only workaround to fix problem where 
		// DatabaseContentProvider does not update the version of the 
		// database.
		db.setVersion(DATABASE_VERSION);
	}
	
	@Override
	public boolean isSyncable() {
		return false;
	}
	
	@Override
	public SyncAdapter getSyncAdapter(Context context) {
		if (m_syncAdapter == null) {
			m_syncAdapter = new AgtdSyncAdapter(context);
		}
		return m_syncAdapter;
	}
	
	@Override
	protected Iterable<? extends AbstractTableMerger> getMergers() {
		// TODO Auto-generated method stub
		return super.getMergers();
	}
	

	private void dumpValues(ContentValues cursor) {
		Set<Entry<String, Object>> headers = cursor.valueSet();
		Log.d(AGtd.LOG_TAG, "RECORD");
		for (Iterator<Entry<String,Object>> itKeys = headers.iterator();itKeys.hasNext();) {
			Entry<String,Object> entry = itKeys.next();
			Log.d(AGtd.LOG_TAG, " Header: " + entry.getKey() + ": " 
					+ entry.getValue());
		}
	}
}
